package com.msunsoft.business.dictionary.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.msunsoft.business.dictionary.mybatis.model.BdOriginDomainDrug;
import com.msunsoft.business.dictionary.service.DrugCompareService;
import com.msunsoft.common.ReponseBodyVo;
import com.msunsoft.util.WebUtils;

@Controller
@RequestMapping(value = "/drugCompare")
public class DrugCompareController {

    @Resource
    private DrugCompareService drugCompareService;

    // private LuceneContentInterface luceneContentInterface;

    // 跳转到药品项目匹配主页面
    @RequestMapping(value = "/drugCompareMain")
    public ModelAndView drugCompareMain(){
        ModelAndView view = new ModelAndView ();
        view.setViewName ("dic-matching/drugCompareMain");
        return view;
    }

    // 跳转到药品项目匹配选择页面，与中心药品统一编码进行匹配
    @RequestMapping(value = "/drugCompareChoose")
    public ModelAndView drugCompareChoose(){
        ModelAndView view = new ModelAndView ();
        view.setViewName ("dic-matching/drugCompareChoose");
        return view;
    }

    /*
     * 获取院内的药品项目信息
     */
    @RequestMapping(value = "/get-origin-drug-by-page")
    public @ResponseBody ReponseBodyVo getOriginDomainDrugPage(HttpServletRequest request){
        // Map map = WebUtils.getPraramsAsMap (request);
        // Page page = new Page ();
        // page.setShowCount (TypeCaseHelper.convert2Integer (map.get ("pagesize")));
        // page.setCurrentPage (TypeCaseHelper.convert2Integer (map.get ("page")));
        // String drugName = request.getParameter ("drugName");
        // String flag = request.getParameter ("flag");
        // Map<String, Object> param = new HashMap<String, Object> ();
        // param.put ("page", page);
        // param.put ("drugName", drugName);
        // param.put ("flag", flag);
        Object data = null;
        String msg = null;
        boolean success = false;
        int Total = 0;
        List<BdOriginDomainDrug> items = drugCompareService.listAll ();
        if (!items.isEmpty ()) {
            msg = "共获取" + items.size () + "条数据!";
            success = true;
            data = items;
            Total = 0;
        } else {
            msg = "未查询到任何数据!";
            success = false;
            data = null;
        }
        return new ReponseBodyVo (true,"成功",msg,data);
    }
    
    /*
     * 根据药品编码查询获取院内的药品项目信息
     */
    @RequestMapping(value = "/get-origin-drug-by-code")
    public ModelAndView getOriginDomainDrugByCode(HttpServletRequest request){
         Map map = WebUtils.getPraramsAsMap (request);
        ModelAndView view = new ModelAndView ();
        view.setViewName ("dic-matching/drugCompareChoose");
        return view;
    }
    /*
     * 确认对照
     */
    @RequestMapping(value = "/beginCompare",produces = "text/html;charset=UTF-8")
    public @ResponseBody String beginChargeItems(HttpServletRequest request){
        String id = request.getParameter ("hosCode");
        String centerid = request.getParameter ("centerCode");
        Map<String, String> map = new HashMap<String, String> ();
        map.put ("id", id);
        map.put ("centerid", centerid);
        int result = drugCompareService.updateBdDrugInfoReg (map);
        if (result == 1) {
            return "{\"result\":\"success\"}";
        } else {
            return "{\"result\":\"fail\"}";
        }
    }

    /*
     * 取消对照
     */
    @RequestMapping(value = "/cancelCompare",produces = "text/html;charset=UTF-8")
    public @ResponseBody String QuxiaoChargeItems(HttpServletRequest request){
        String id = request.getParameter ("id");
        String centerId = null;
        Map<String, String> map = new HashMap<String, String> ();
        map.put ("id", id);
        map.put ("centerId", centerId);
        int result = drugCompareService.updateRegCancel (map);
        if (result == 1) {
            return "{\"result\":\"success\"}";
        } else {
            return "{\"result\":\"fail\"}";
        }
    }

    // // 智能匹配
    // public @ResponseBody String zhinengPiPei(HttpServletRequest request){
    // try {
    // BdDatasetDictonaryItem item = new BdDatasetDictonaryItem ();
    // item.setDictionaryCode ("CS_CHECK_ITEM");
    // // item.setKeyCode("210kl013");
    // item.setValueAlias ("乳腺099877hjlkkkllk钼靶");
    // List<BdDatasetDictonaryItem> list = luceneContentInterface.searchList (item, 0, 100);
    // for ( BdDatasetDictonaryItem bdDatasetDictonaryItem : list ) {
    // System.out.println (bdDatasetDictonaryItem);
    // }
    // } catch (IOException e) {
    // e.printStackTrace ();
    // } catch (InvalidTokenOffsetsException e) {
    // e.printStackTrace ();
    // }
    // return null;
    // }

    // 删除医院药品字典值项
    @RequestMapping(value = "/deleteDrugItem",produces = "text/html;charset=UTF-8")
    public @ResponseBody String deleteHosDrug(HttpServletRequest request){
        String drugCode = request.getParameter ("drugCode");
        Map<String, String> map = new HashMap<String, String> ();
        map.put ("drugCode", drugCode);
        int result = drugCompareService.deleteDrugItem (map);
        if (result == 1) {
            return "{\"result\":\"success\"}";
        } else {
            return "{\"result\":\"fail\"}";
        }
    }

    // 跳转到修改页面
    @RequestMapping(value = "toModifyMain",produces = "text/html;charset=UTF-8")
    public ModelAndView ModifyCharges(HttpServletRequest request){
        String drugCode = request.getParameter ("drugCode");
        Map<String, String> map = new HashMap<String, String> ();
        map.put ("drugCode", drugCode);
        BdOriginDomainDrug entity = drugCompareService.selectModify (map);
        ModelAndView view = new ModelAndView ();
        view.addObject ("entity", entity);
        view.setViewName ("dic-matching/modifyDrugCompare");
        return view;
    }

    // 保存修改的内容
    @RequestMapping(value = "saveModify",produces = "text/html;charset=UTF-8")
    public @ResponseBody String saveModifyDrug(HttpServletRequest request){
        String drugCode = request.getParameter ("drugCode");
        String drugName = request.getParameter ("drugName");
        String hosDrugCode = request.getParameter ("hosDrugCode");
        Map<String, String> map = new HashMap<String, String> ();
        map.put ("drugCode", drugCode);
        map.put ("drugName", drugName);
        map.put ("hosDrugCode", hosDrugCode);
        int result = drugCompareService.saveModify (map);
        if (result == 1) {
            return "{\"result\":\"success\"}";
        } else {
            return "{\"result\":\"fail\"}";
        }

    }
}
